#!/bin/bash

# 平台标识符工具函数
PLATFORM_FILE="PLATFORM_IDENTIFIERS.txt"

# 加载平台标识符
load_platforms() {
    if [ ! -f "$PLATFORM_FILE" ]; then
        echo "错误: 平台标识符文件不存在: $PLATFORM_FILE"
        return 1
    fi
    return 0
}

# 获取所有平台
get_all_platforms() {
    grep -E "^[a-zA-Z0-9_.-]+$" "$PLATFORM_FILE" | grep -v "^#"
}

# 按类别获取平台
get_platforms_by_category() {
    local category="$1"
    case "$category" in
        "windows") grep -A 10 "## Windows" "$PLATFORM_FILE" | grep -E "^[a-zA-Z0-9_.-]+$" ;;
        "linux") grep -A 10 "## Linux" "$PLATFORM_FILE" | grep -E "^[a-zA-Z0-9_.-]+$" ;;
        "android") grep -A 10 "## Android" "$PLATFORM_FILE" | grep -E "^[a-zA-Z0-9_.-]+$" ;;
        "macos") grep -A 10 "## macOS" "$PLATFORM_FILE" | grep -E "^[a-zA-Z0-9_.-]+$" ;;
        "embedded") grep -A 10 "## 嵌入式" "$PLATFORM_FILE" | grep -E "^[a-zA-Z0-9_.-]+$" ;;
        "rtos") grep -A 10 "## 实时操作系统" "$PLATFORM_FILE" | grep -E "^[a-zA-Z0-9_.-]+$" ;;
        "mcu") grep -A 10 "## 微控制器" "$PLATFORM_FILE" | grep -E "^[a-zA-Z0-9_.-]+$" ;;
        "dsp") grep -A 10 "## DSP" "$PLATFORM_FILE" | grep -E "^[a-zA-Z0-9_.-]+$" ;;
        "fpga") grep -A 10 "## FPGA" "$PLATFORM_FILE" | grep -E "^[a-zA-Z0-9_.-]+$" ;;
        "gpu") grep -A 10 "## GPU" "$PLATFORM_FILE" | grep -E "^[a-zA-Z0-9_.-]+$" ;;
        *) get_all_platforms ;;
    esac
}

# 搜索平台
search_platform() {
    local keyword="$1"
    grep -i "$keyword" "$PLATFORM_FILE" | grep -E "^[a-zA-Z0-9_.-]+$"
}

# 验证平台标识符
validate_platform() {
    local platform="$1"
    if get_all_platforms | grep -q "^$platform$"; then
        echo "✅ 有效的平台标识符: $platform"
        return 0
    else
        echo "❌ 无效的平台标识符: $platform"
        return 1
    fi
}

# 生成UEStudio目标
generate_uestudio_target() {
    local platform="$1"
    local variant="${2:-release}"
    local version="${3:-v1}"
    
    # 从平台标识符提取信息
    local arch=$(echo "$platform" | cut -d'-' -f1)
    local vendor=$(echo "$platform" | cut -d'-' -f2)
    local system=$(echo "$platform" | cut -d'-' -f3)
    
    # 映射系统名称
    case "$system" in
        "windows") system="windows" ;;
        "linux") system="linux" ;;
        "android") system="android" ;;
        "darwin") system="macos" ;;
        *) system="unknown" ;;
    esac
    
    # 映射工具链
    case "$vendor" in
        "mingw32") toolchain="mingw" ;;
        "pc") toolchain="msvc" ;;
        "nvidia") toolchain="cuda" ;;
        "amd") toolchain="rocm" ;;
        "intel") toolchain="oneapi" ;;
        "apple") toolchain="clang" ;;
        *) toolchain="gcc" ;;
    esac
    
    echo "${system}-${arch}-${toolchain}-${variant}-${version}"
}

# 显示平台信息
show_platform_info() {
    local platform="$1"
    echo "平台: $platform"
    echo "架构: $(echo "$platform" | cut -d'-' -f1)"
    echo "供应商: $(echo "$platform" | cut -d'-' -f2)"
    echo "系统: $(echo "$platform" | cut -d'-' -f3)"
    echo "运行时: $(echo "$platform" | cut -d'-' -f4)"
    
    local uestudio_target=$(generate_uestudio_target "$platform")
    echo "UEStudio目标: $uestudio_target"
}

# 主菜单
main_menu() {
    echo "🎯 UEStudio 平台标识符工具"
    echo "=========================================="
    echo "1. 显示所有平台"
    echo "2. 按类别显示平台"
    echo "3. 搜索平台"
    echo "4. 验证平台标识符"
    echo "5. 生成UEStudio目标"
    echo "6. 显示平台信息"
    echo "7. 退出"
    echo ""
    
    read -p "请选择操作 (1-7): " choice
    
    case $choice in
        1) get_all_platforms ;;
        2) 
            echo "可用类别: windows, linux, android, macos, embedded, rtos, mcu, dsp, fpga, gpu"
            read -p "请输入类别: " category
            get_platforms_by_category "$category"
            ;;
        3)
            read -p "请输入搜索关键词: " keyword
            search_platform "$keyword"
            ;;
        4)
            read -p "请输入平台标识符: " platform
            validate_platform "$platform"
            ;;
        5)
            read -p "请输入平台标识符: " platform
            read -p "请输入变体 [release]: " variant
            read -p "请输入版本 [v1]: " version
            variant=${variant:-release}
            version=${version:-v1}
            generate_uestudio_target "$platform" "$variant" "$version"
            ;;
        6)
            read -p "请输入平台标识符: " platform
            show_platform_info "$platform"
            ;;
        7)
            echo "再见!"
            exit 0
            ;;
        *)
            echo "无效选择"
            ;;
    esac
    
    echo ""
    read -p "按回车键继续..."
    main_menu
}

# 初始化
if load_platforms; then
    main_menu
fi
